package study.android;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class LocalDb {
	public final static String table__terminal_master = "terminal_master";
	public final static String table__tq_fence = "tq_fence";
	public final static String table__contact_phone = "contact_phone";
	public final static String table__terminal_param = "terminal_param";


	private DBHelper opener;
	private SQLiteDatabase db;

	public SQLiteDatabase getDb() {
		return db;
	}

	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}

	Context context;
	String dbFname;

	public LocalDb(Context context) {
		this.context = context;
		dbFname = g.getDataPath() + "/" + g.AppCode + ".sqlite";

		// this.opener = new DBHelper(context);
		this.opener = new DBHelper(context);
		db = opener.getWritableDatabase();
	}

	public void recreate(){
		db.execSQL("drop table if exists terminal_master");
		db.execSQL("drop table if exists contact_phone");
		db.execSQL("drop table if exists tq_fence");
		db.execSQL("drop table if exists terminal_param");
		opener.onCreate(db);
	}

	class DBHelper extends SQLiteOpenHelper {
		public static final int version = 2;

		@SuppressWarnings("deprecation")
		public String stringFromAssets(String fileName) {
			StringBuilder ReturnString = new StringBuilder();
			InputStream fIn = null;
			InputStreamReader isr = null;
			BufferedReader input = null;
			try {
				fIn = context.getResources().getAssets().open(fileName, Context.MODE_WORLD_READABLE);
				isr = new InputStreamReader(fIn);
				input = new BufferedReader(isr);
				String line = "";
				while ((line = input.readLine()) != null) {
					ReturnString.append(line);
				}
			} catch (Exception e) {
				e.getMessage();
			} finally {
				try {
					if (isr != null)
						isr.close();
					if (fIn != null)
						fIn.close();
					if (input != null)
						input.close();
				} catch (Exception e2) {
					e2.getMessage();
				}
			}
			return ReturnString.toString();
		}

		public DBHelper(Context context) {
			super(context, LocalDb.this.dbFname, null, DBHelper.version);
		}

		// onCreate is called once if database not exists.
		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.w(g.AppCode, "LocalDb onCreate()");
			db.execSQL(this.stringFromAssets("sql/1001.terminal_master.ddl"));
			db.execSQL(this.stringFromAssets("sql/1002.contact_phone.ddl"));
			db.execSQL(this.stringFromAssets("sql/1003.tq_fence.ddl"));
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(g.AppCode, "Database Version : Old Version:" + oldVersion + "  New Version:" + newVersion);
			if (oldVersion == 1) {
				Log.e(g.AppCode, "Old Version:" + oldVersion + "  New Version:" + newVersion);
			}
		}
	}

	// public void execSQL(String sql) {
	// db.execSQL(sql);
	// }
	//
	// public Cursor rawQuery(String sql, String[] selectionArgs) {
	// Cursor cursor = db.rawQuery(sql, selectionArgs);
	// return cursor;
	// }
}
